<?php
class Khachhang_AdminItemController extends Zendvn_Controller_Action{
	
	protected $_arrParam;
	protected $_currentController;
	protected $_actionMain;
	
	protected $_paginator = array(
									'itemCountPerPage' => 15,
									'pageRange' => 10,
									'currentPage' => 1
									);
	protected $_namespace;
	
	protected $_page = '';
	
	public function init() {
		$this->_arrParam = $this->_request->getParams();
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller'] . '/index';	
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		
		//Lay thong tin so phan tu tren mot trang
		if (isset($this->_arrParam['limitPage'])) {
			$ssFilter->limitPage = $this->_request->getParam('limitPage');
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}elseif (!empty($ssFilter->limitPage)) {
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}
		
		//Trang hien tai
		if (isset($this->_arrParam['page'])) {
			$this->_paginator['currentPage'] = $this->_arrParam['page'];
			$this->_page = '/page/' . $this->_arrParam['page'];
		}
		if (isset($this->_arrParam['tn'])) {
			$this->_page .= '/tn/' . $this->_arrParam['tn'];
		} else {
			$this->_arrParam['tn'] = 0;
			$this->_page .= '/tn/' . $this->_arrParam['tn'];
		}
		
		//Truyen thong tin phan trang vao mang du lieu
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//$ssFilter->unsetAll();
		if (empty($ssFilter->col)) {
			$ssFilter->keywords 	= '';
			$ssFilter->tungay	 	= '';
			$ssFilter->denngay	 	= '';
			$ssFilter->col 			= 'kh.id';
			$ssFilter->order 		= 'DESC';
			$ssFilter->id_cat 		= 0;
			$ssFilter->id_phongban 	= 0;
			$ssFilter->id_nhanvien	= 0;
			$ssFilter->id_nguon_dk	= 0;
		}
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		$this->_arrParam['ssFilter']['tungay'] 		= $ssFilter->tungay;
		$this->_arrParam['ssFilter']['denngay'] 	= $ssFilter->denngay;
		$this->_arrParam['ssFilter']['col'] 		= $ssFilter->col;
		$this->_arrParam['ssFilter']['order'] 		= $ssFilter->order;
		$this->_arrParam['ssFilter']['id_cat'] 		= $ssFilter->id_cat;
		$this->_arrParam['ssFilter']['id_phongban']	= $ssFilter->id_phongban;
		$this->_arrParam['ssFilter']['id_nhanvien']	= $ssFilter->id_nhanvien;
		$this->_arrParam['ssFilter']['id_nguon_dk']	= $ssFilter->id_nguon_dk;
		
		//Truyen ra ngoai view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	
		$siteConfig = Zend_Registry::get('siteConfig');
		$template_path = TEMPLATE_PATH . "/admin/" . $siteConfig['template']['admin'];
		$this->loadTemplate($template_path, 'template.ini', 'customer');
	}

	public function indexAction() {
		if ($this->_arrParam['tn'] == 0) {
			$this->view->Title = 'Khách hàng không theo dõi :: Danh sách';
		} else {
			$this->view->Title = 'Khách hàng tiềm năng :: Danh sách';
		}
		$this->view->headTitle($this->view->Title, true);
		
		$tblKhachhang = new Khachhang_Model_Item();
		$this->view->Items = $tblKhachhang->listItem($this->_arrParam, array('task'=>'admin-list'));
		
		$this->view->slbCategory = $tblKhachhang->itemInSelectbox($this->_arrParam);
		$this->view->slbNguondk  = $tblKhachhang->itemInSelectbox($this->_arrParam, array('task'=>'admin-nguondk'));
		$tblAjax = new Khachhang_Model_Ajax();
		$this->view->slbPhongban = $tblAjax->itemInSelectbox($this->_arrParam, array('task' => 'get-phongban'));
		$this->view->slbNhanvien = $tblAjax->itemInSelectbox(array('id_phongban' => $this->_arrParam['ssFilter']['id_phongban']), array('task' => 'get-nhanvien'));
		
		$totalItem = $tblKhachhang->countItem($this->_arrParam, array('task'=>'admin-list'));
		
		$paginator = new Zendvn_Paginator();
		$this->view->paginator = $paginator->createPaginator($totalItem, $this->_paginator);
	}
	
	public function filterAction() {
		
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
	
		if ($this->_arrParam['type'] == 'search') {
			if ($this->_arrParam['key'] == 1) {
				$ssFilter->keywords = trim($this->_arrParam['keywords']);
				$ssFilter->tungay = trim($this->_arrParam['tungay']);
				$ssFilter->denngay = trim($this->_arrParam['denngay']);
			}else{
				$ssFilter->keywords = '';
				$ssFilter->tungay	 = '';
				$ssFilter->denngay	 = '';
			}
		}
		
		if ($this->_arrParam['type'] == 'id_cat') {
			$ssFilter->id_cat = $this->_arrParam['id_cat'];
		}
		
		if ($this->_arrParam['type'] == 'id_nguon_dk') {
			$ssFilter->id_nguon_dk = $this->_arrParam['id_nguon_dk'];
		}
		
		if ($this->_arrParam['type'] == 'id_phongban') {
			$ssFilter->id_phongban = $this->_arrParam['id_phongban'];
			$ssFilter->id_nhanvien = 0;
		}
		
		if ($this->_arrParam['type'] == 'id_nhanvien') {
			$ssFilter->id_nhanvien = $this->_arrParam['id_nhanvien'];
		}
	
		if ($this->_arrParam['type'] == 'order') {
			$ssFilter->col = $this->_arrParam['col'];
			$ssFilter->order = $this->_arrParam['by'];
		}
	
		$this->_redirect('khachhang/admin-item/index/tn/' . $this->_arrParam['tn']);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function deleteAction() {
		if ($this->_arrParam['tn'] == 0) {
			$this->view->Title = 'Khách hàng không theo dõi:: Xóa';
		} else {
			$this->view->Title = 'Khách hàng tiềm năng:: Xóa';
		}
		$this->view->headTitle($this->view->Title, true);
		
		if($this->_request->isPost()){
			if(($this->_arrParam['type'] == 'multi-delete') && ($this->_arrParam['task'] == 'ok')){
				$tblKhachhang = new Khachhang_Model_Item();
				$tblKhachhang->deleteItem($this->_arrParam, array('task'=>'admin-delete-muti'));
				$this->_redirect($this->_actionMain . $this->_page . '/save/ok');
			}else if(!empty($this->_arrParam['id'])){
				$tblKhachhang = new Khachhang_Model_Item();
				$tblKhachhang->deleteItem($this->_arrParam, array('task'=>'admin-delete'));
				$this->_redirect($this->_actionMain . $this->_page . '/save/ok');
			}
		}
	}
	
	public function statusAction() {
		$tblKhachhang = new Khachhang_Model_Item();
		$tblKhachhang->changeStatus($this->_arrParam);
		if (isset($this->_arrParam['tn'])) {
			$this->_redirect($this->_actionMain . $this->_page . '/tn/' . $this->_arrParam['tn']);
		} else { 
			$this->_redirect($this->_currentController . '/indexkh' . $this->_page);
		}
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function sortAction() {
		if ($this->_request->isPost()) {
			$tblKhachhang = new Khachhang_Model_Item();
			$tblKhachhang->sortItem($this->_arrParam, array('task'=>'admin-sort'));
			if (isset($this->_arrParam['tn']))
				$this->_redirect($this->_actionMain . $this->_page . '/tn/' . $this->_arrParam['tn']);
			else
				$this->_redirect($this->_currentController . '/indexkh' . $this->_page);
		}
		$this->_helper->viewRenderer->setNoRender();
	}
	
	
	
	public function excelAction(){
	
		$ssFilter = $this->_arrParam['ssFilter'];
	
		$tblClb = new Khachhang_Model_Item();
		$this->view->Items = $listKhachhangclb = $tblClb->listItem($this->_arrParam, array('task'=>'admin-list'));
// 		echo '<pre>';
// 		print_r($listKhachhangclb);
// 		echo '</pre>';die;
		$info = new Zendvn_System_Info();
		$userInfo = $info->getMemberInfo();
	
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
	
		error_reporting(E_ALL);
		ini_set('display_errors', TRUE);
		ini_set('display_startup_errors', TRUE);
		date_default_timezone_set('Asia/Ho_Chi_Minh');
	
		if (PHP_SAPI == 'cli')
			die('This example should only be run from a Web Browser');
	
		/** Include PHPExcel */
		require_once SCRIPTS_PATH . '/phpexcel/PHPExcel.php';
	
		// Create new PHPExcel object
		$objPHPExcel = new PHPExcel();
	
		// Set document properties
		$objPHPExcel->getProperties()->setCreator($userInfo['user_name'])
		->setLastModifiedBy($userInfo['user_name'])
		->setTitle("Danh sach khach hang")
		->setSubject("Danh sach khach hang")
		->setDescription("Danh sach khach hang duoc xuat boi " . $userInfo['user_name'])
		->setKeywords($userInfo['user_name'])
		->setCategory($userInfo['user_name']);
	
		// Tiêu đề
		$objPHPExcel->getActiveSheet()->setCellValueExplicit('B2', 'DANH SÁCH KHÁCH HÀNG')->mergeCells('B2:G2');
		$objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->setSize(20)->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
		$objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS);
	
		// Fillter
		$start = 4;
		$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue('B'.$start, 'Ngày xuất: '.@date('d/m/Y h:i:s'))->mergeCells('B'.$start.':C'.$start);
		$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue('D'.$start, 'Bởi: '.$userInfo['user_name'])->mergeCells('D'.$start.':E'.$start);
	
	
	
		// Tiêu đề
		$title = $start + 2;
		$objPHPExcel->getActiveSheet()->setCellValueExplicit('B'.$title, 'STT');
		$objPHPExcel->getActiveSheet()->getStyle('B'.$title)->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('B'.$title)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('B'.$title)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	
		$objPHPExcel->getActiveSheet()->setCellValueExplicit('C'.$title, 'Họ và tên');
		$objPHPExcel->getActiveSheet()->getStyle('C'.$title)->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('C'.$title)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('C'.$title)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	
		$objPHPExcel->getActiveSheet()->setCellValueExplicit('D'.$title, 'Điện thoại');
		$objPHPExcel->getActiveSheet()->getStyle('D'.$title)->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('D'.$title)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('D'.$title)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	
		$objPHPExcel->getActiveSheet()->setCellValueExplicit('E'.$title, 'Email');
		$objPHPExcel->getActiveSheet()->getStyle('E'.$title)->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('E'.$title)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('E'.$title)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	
		$objPHPExcel->getActiveSheet()->setCellValueExplicit('F'.$title, 'Địa chỉ');
		$objPHPExcel->getActiveSheet()->getStyle('F'.$title)->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('F'.$title)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('F'.$title)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	
		$objPHPExcel->getActiveSheet()->setCellValueExplicit('G'.$title, 'Ngày thêm mới');
		$objPHPExcel->getActiveSheet()->getStyle('G'.$title)->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('G'.$title)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('G'.$title)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	
		$objPHPExcel->getActiveSheet()->setCellValueExplicit('H'.$title, 'Người quản lý');
		$objPHPExcel->getActiveSheet()->getStyle('H'.$title)->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('H'.$title)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('H'.$title)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	
		$objPHPExcel->getActiveSheet()->setCellValueExplicit('I'.$title, 'Cập nhật lần cuối');
		$objPHPExcel->getActiveSheet()->getStyle('I'.$title)->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('I'.$title)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('I'.$title)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
// 		$objPHPExcel->getActiveSheet()->setCellValueExplicit('J'.$title, 'ID người cập nhật');
// 		$objPHPExcel->getActiveSheet()->getStyle('J'.$title)->getFont()->setBold(true);
// 		$objPHPExcel->getActiveSheet()->getStyle('J'.$title)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 		$objPHPExcel->getActiveSheet()->getStyle('J'.$title)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
	
	
		//Chieu cao cột title
		$objPHPExcel->getActiveSheet()->getRowDimension($title)->setRowHeight(20);
	
		// Chiều rộng cột
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
		$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(35);
		$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(35);
		$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(35);
		$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(35);
		$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(35);
		$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(35);
// 		$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(35);
		//Lấy danh sach các thành viên
		if(count($listKhachhangclb) > 0){
			foreach ($listKhachhangclb AS $key => $val){
				$number = $title + $key + 1;
				$objPHPExcel->getActiveSheet()->setCellValueExplicit('B'.$number, $key + 1);
				$objPHPExcel->getActiveSheet()->getStyle('B'.$number)->getFont();
				$objPHPExcel->getActiveSheet()->getStyle('B'.$number)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$objPHPExcel->getActiveSheet()->getStyle('B'.$number)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	
				$objPHPExcel->getActiveSheet()->setCellValueExplicit('C'.$number, $val['name']);
				$objPHPExcel->getActiveSheet()->getStyle('C'.$number)->getFont();
				$objPHPExcel->getActiveSheet()->getStyle('C'.$number)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$objPHPExcel->getActiveSheet()->getStyle('C'.$number)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
	
				$objPHPExcel->getActiveSheet()->setCellValueExplicit('D'.$number, $val['phone']);
				$objPHPExcel->getActiveSheet()->getStyle('D'.$number)->getFont();
				$objPHPExcel->getActiveSheet()->getStyle('D'.$number)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$objPHPExcel->getActiveSheet()->getStyle('D'.$number)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
	
				$objPHPExcel->getActiveSheet()->setCellValueExplicit('E'.$number, $val['email']);
				$objPHPExcel->getActiveSheet()->getStyle('E'.$number)->getFont();
				$objPHPExcel->getActiveSheet()->getStyle('E'.$number)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$objPHPExcel->getActiveSheet()->getStyle('E'.$number)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
	
				$objPHPExcel->getActiveSheet()->setCellValueExplicit('F'.$number, $val['address']);
				$objPHPExcel->getActiveSheet()->getStyle('F'.$number)->getFont();
				$objPHPExcel->getActiveSheet()->getStyle('F'.$number)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$objPHPExcel->getActiveSheet()->getStyle('F'.$number)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
	
				$objPHPExcel->getActiveSheet()->setCellValueExplicit('G'.$number, $val['created']);
				$objPHPExcel->getActiveSheet()->getStyle('G'.$number)->getFont();
				$objPHPExcel->getActiveSheet()->getStyle('G'.$number)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$objPHPExcel->getActiveSheet()->getStyle('G'.$number)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	
				$objPHPExcel->getActiveSheet()->setCellValueExplicit('H'.$number, $val['quanly_name']);
				$objPHPExcel->getActiveSheet()->getStyle('H'.$number)->getFont();
				$objPHPExcel->getActiveSheet()->getStyle('H'.$number)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$objPHPExcel->getActiveSheet()->getStyle('H'.$number)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
	
				$objPHPExcel->getActiveSheet()->setCellValueExplicit('I'.$number, $val['modified']);
				$objPHPExcel->getActiveSheet()->getStyle('I'.$number)->getFont();
				$objPHPExcel->getActiveSheet()->getStyle('I'.$number)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$objPHPExcel->getActiveSheet()->getStyle('I'.$number)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
	
// 				$db = Zend_Registry::get('connectDb');
// 				$sql ='SELECT name FROM users WHERE id=' .$val['modified_by'];
// 				$nameUser= $db->query($sql);
				
// 				$objPHPExcel->getActiveSheet()->setCellValueExplicit('J'.$number, 'NV'.$val['modified_by']);
// 				$objPHPExcel->getActiveSheet()->getStyle('J'.$number)->getFont();
// 				$objPHPExcel->getActiveSheet()->getStyle('J'.$number)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 				$objPHPExcel->getActiveSheet()->getStyle('J'.$number)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
	
	
	
	
			}
		}
	
		// Rename worksheet
		//$objPHPExcel->getActiveSheet()->setTitle('Simple');
	
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);
	
	
		// Redirect output to a client’s web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$userInfo['user_name'].'_'.@date('d_m_Y').'.xls"');
		header('Cache-Control: max-age=0');
	
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
		exit;
	
	}

}



